import api from '@/api'; // 直接导入独立的 api 模块
import cookies from 'js-cookie'

export default {
	namespaced: true,
	actions: {
		checkLoginStatus(context) {
			const token = cookies.get('qizhi_token');
			
			if (token) {
				return api.serviceUser.post('api/user/checkToken', null, {
					headers: {
					  'Authorization': `Bearer ${token}`
					}
				})
				.then(response => {
					if (response.data.code == "200") {
						context.commit('SET_LOGIN_STATUS', true)
				    	context.commit('SET_USER_ACCOUNT', response.data.data.userAccount || '')

						const now = new Date();
						now.setTime(now.getTime() + 2 * 60 * 60 * 1000); // 当前时间 + 60 * 60 * 2 秒
						cookies.set('qizhi_token', response.data.data.token, { expires: now })
						return true
					} else if (response.data.code == "40905") {
						context.commit('SET_LOGIN_STATUS', false)
				    	context.commit('SET_USER_ACCOUNT', response.data.data.userAccount || '')
						return false
					} else {
						context.commit('SET_LOGIN_STATUS', false)
				    	context.commit('SET_USER_ACCOUNT', response.data.data.userAccount || '')
						return false
					}
				})
				.catch(error => {
				  context.commit('SET_LOGIN_STATUS', false)
				  context.commit('SET_USER_ACCOUNT', '')
				  throw error
				})
			} else {
				context.commit('SET_LOGIN_STATUS', false)
				context.commit('SET_USER_ACCOUNT', '')
				return false
			}
		},
		login(context, userLoginVo) {
			const now = new Date();
      		now.setTime(now.getTime() + 2 * 60 * 60 * 1000); // cookie 有效期两个小时
      		cookies.set('qizhi_token', userLoginVo.data.token, { expires: now })
			context.commit('SET_LOGIN_STATUS', true)
			context.commit('SET_USER_ACCOUNT', userLoginVo.data.userAccount || '')
		},
		logout(context){
			const token = cookies.get('qizhi_token');
	  
			// 有可能用户在登陆状态下，token已经失效了
			if (token) {
				api.serviceUser.post('api/user/logout', null, {
					headers: {
						'Authorization': `Bearer ${token}`
					}
				})
				.then(response => {
					if (response.data.code == "200") {
						context.commit('SET_LOGIN_STATUS', false)
						context.commit('SET_USER_ACCOUNT', '')
						cookies.remove('qizhi_token')
					} else if (response.data.code == "40907") {
						context.commit('SET_LOGIN_STATUS', false)
						context.commit('SET_USER_ACCOUNT', '')
						cookies.remove('qizhi_token')
					} else {
						context.commit('SET_LOGIN_STATUS', false)
						context.commit('SET_USER_ACCOUNT', '')
						cookies.remove('qizhi_token')
					}
				})
				.catch(error => {
					context.commit('SET_LOGIN_STATUS', false)
					context.commit('SET_USER_ACCOUNT', '')
					cookies.cookies.remove('qizhi_token')
					throw error
				})
			} else {
				context.commit('SET_LOGIN_STATUS', false)
				context.commit('SET_USER_ACCOUNT', '')
				cookies.remove('qizhi_token')
			}
		}
	},
	mutations: {
	  SET_LOGIN_STATUS(state, payload) {
		state.isLogin = payload
	  },
	  SET_USER_ACCOUNT(state, payload) {
		state.userAccount = payload
	  }
	},
	state: {
		isLogin: false,
		userAccount: ""
	},
	getters: {
	  isLoggedIn: state => state.isLogin
	}
  }